package com.streambolics.worseminton.data;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import com.streambolics.data.Person;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(strategy = InheritanceStrategy.NEW_TABLE)
public class LocationInterest extends WorsemintonGuestObject
{
    @Persistent
    long             locationId;

    @Persistent
    Boolean          interested;

    @Persistent
    Boolean          autoInvited;

    @Persistent
    Boolean          sendGuestChanges;

    @Persistent
    Long             forwardId;

    @NotPersistent
    private Location _Location;

    /**
     * Create a new LocationInterest for the given location and guest.
     * 
     * @param aLocationId
     * @param aGuestId
     */

    protected LocationInterest(long aLocationId, long aGuestId)
    {
        super (aGuestId);
        locationId = aLocationId;
        interested = true;
        sendGuestChanges = true;
        autoInvited = true;
        forwardId = (long) -1;
    }

    public static LocationInterest access (PersistenceManager aDatabase, long aLocationId, long aGuestId)
    {
        String query = "select from " + LocationInterest.class.getName () + " where locationId == p_location && guestId == p_guestId";
        Query q = aDatabase.newQuery (query);
        q.declareParameters ("Long p_location, Long p_guestId");
        @SuppressWarnings("unchecked")
        List<LocationInterest> found = (List<LocationInterest>) q.execute (aLocationId, aGuestId);

        if (found.size () > 0)
        {
            LocationInterest l = found.get (0);
            l.setDatabase (aDatabase);
            return l;
        }
        else
        {
            return create (aDatabase, aLocationId, aGuestId);
        }
    }

    /**
     * Create a new LocationInterest for a location and a guest. The interest
     * will be made persistent, but not check is made to see if the record
     * already exists. It is strongly advised to always call access() instead.
     * 
     * @param aDatabase
     * @param aLocationId
     * @param aGuestId
     * @return
     */

    public static LocationInterest create (PersistenceManager aDatabase, long aLocationId, long aGuestId)
    {
        LocationInterest l = new LocationInterest (aLocationId, aGuestId);
        l.setDatabase (aDatabase);
        aDatabase.makePersistent (l);
        return l;
    }

    public Person getForward ()
    {
        if (isForwarded ())
        {
            return Person.load (getDatabase (), getForwardId ());
        }
        else
        {
            return getGuest ();
        }
    }

    public long getForwardId ()
    {
        return forwardId == null ? -1 : forwardId;
    }

    public Location getLocation ()
    {
        if (_Location == null)
        {
            _Location = Location.load (getDatabase (), locationId);
        }
        return _Location;
    }

    @Override
    public String getNameHtmlStyle ()
    {
        if (isInterested ())
        {
            return "invitationinvited";
        }
        else
        {
            return "invitationnotinvited";
        }
    }

    public boolean isAutoInvited ()
    {
        return autoInvited == null ? true : autoInvited;
    }

    public boolean isForwarded ()
    {
        return forwardId == null ? false : forwardId > 0;
    }

    public boolean isInterested ()
    {
        return interested == null ? true : interested;
    }

    public void setAutoInvited (boolean aAutoInvited)
    {
        autoInvited = aAutoInvited;
    }

    public void setForwardId (long aForwardId)
    {
        forwardId = aForwardId;
    }

    public void setInterested (boolean aInterested)
    {
        interested = aInterested;
    }

    public void setSendGuestChanges (boolean aSendGuestChanges)
    {
        sendGuestChanges = aSendGuestChanges;
    }

    public boolean shouldSendGeneralChanges ()
    {
        return true;
    }

    public boolean shouldSendGuestChanges ()
    {
        return sendGuestChanges;
    }
}
